package com.google.gwt.dev;

import com.google.gwt.core.ext.TreeLogger;

import java.util.ArrayList;

// TODO: Need more features
public class InMemoryLogger extends TreeLogger {

  private static class LogEntry {
    private Type type;
    private String msg;
    private Throwable caught;
    private HelpInfo helpInfo;

    public LogEntry(Type type, String msg, Throwable caught, HelpInfo helpInfo) {
      super();
      this.type = type;
      this.msg = msg;
      this.caught = caught;
      this.helpInfo = helpInfo;
    }

  }

  private boolean disconnected = false;
  private final ArrayList<LogEntry> logData = new ArrayList<LogEntry>();
  private String name;

  public InMemoryLogger(String name) {
    this.name = name;
  }

  @Override
  public TreeLogger branch(Type type, String msg, Throwable caught,
      HelpInfo helpInfo) {
    return this;
  }

  @Override
  public boolean isLoggable(Type type) {
    return Type.TRACE.isLowerPriorityThan(type);
  }

  @Override
  public void log(Type type, String msg, Throwable caught, HelpInfo helpInfo) {
    // TODO: Cannot insert messages forever, need to clean-up once in a while
    this.logData.add(new LogEntry(type, msg, caught, helpInfo));
  }

  public boolean isDisconnected() {
    return disconnected;
  }

  public void setDisconnected(boolean disconnected) {
    this.disconnected = disconnected;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}
